<?php
$loader->requireOnce('includes/inPatientCore/InPatientCoreErrorCode.class.php');
$loader->requireOnce('includes/inPatientCore/InPatientCoreException.class.php');
$loader->requireOnce('includes/CoreErrorCode.class.php');
$loader->requireOnce('includes/CoreException.class.php');
$loader->requireOnce('includes/EnumerationConstant.class.php');
$loader->requireOnce('includes/EmerQueueFactory.class.php');
$loader->requireOnce('includes/EmergencyInstantPanel.class.php');

$loader->requireOnce('includes/EmerProcessor.class.php');



/**
 * Controler for processing emergency general diagnosis panel.
 * @package module.dept_emer.controllers
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class C_GeneralDiagnosis extends Controller {
	private $logger ;
	private $tpProcessor ;
	function __construct($template_mod = "general") {
		parent::Controller();
		$this->logger = LoggerManager::getLogger('module.dept_emer.controllers.C_GeneralDiagnosis');
		$this->template_mod = $template_mod;
		$this->view->path="patient_instant_info";
		$this->tpProcessor = new EmerProcessor($this);
	}
	/**
	 * Build patient info form.
	 * @param $_GET['ticketId']
	 * @return jsonObject patient info.
	 */
	public function actionloadGeneralDiagnosisAjax() {
		$result = array('error'=>array('code'=>0,'mess'=>''));
		$ticketId = $_GET['ticketId'];

		$this->logger->info('Start load loadGeneralDiagnosis for patient has ticket ['. $ticketId .']');
		try {

			$result['result'] = $this->tpProcessor->loadIPGeneralDiagnosis($ticketId);

		} catch (InpatientCoreException $ex) {

			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('Load GeneralDiagnosis for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		}   catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('Load GeneralDiagnosis for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
        }
		$this->logger->info('Finish loadGeneralDiagnosis for patient has ticket ['. $ticketId .']');
		return json_encode($result);
	}

	/**
	 * Build patient info form.
	 * @param $_GET['ticketId']
	 * @return jsonObject patient info.
	 */
	public function actionSaveGeneralDiagnosisAjax() {
		$result = array('error'=>array('code'=>0,'mess'=>'','field'=>''),'result'=>array());
		$ticketId = $_GET['ticketId'];

		$this->logger->info('Start actionSaveGeneralDiagnosisAjax for patient has ticket ['. $ticketId .']');
		try {
			// 1. Save general diagnosis information
			$generalDiagnosis = array(
	        	'ticket_id'=>$ticketId,
	        	'glucose'=>Utils::getNumberParaFromGet('diagnosisGlu'),
	        	'enum_painful_status'=> $_GET['diagnosisPainful'],
	        	'allergy'=>$_GET['diagnosisAllergy'],
	        	'general_allergy_detail'=>$_GET['diagnosisAllergyDetail'],
				'drug_allergy_detail' =>  $_GET['diagnosisDrugAllergyDetail'],
	        	'enum_glasgow_e'=>$_GET['diagnosisGE'],
	        	'enum_glasgow_v'=>$_GET['diagnosisGV'],
        		'enum_glasgow_m'=>$_GET['diagnosisGM']
			);			
			$result['result']['generalDiagnosisId']=$this->tpProcessor->saveGeneralDiagnosis($generalDiagnosis);
			
			// 2. Save in-patient emergency information  
			// 2.a Get ICD10
			$icd10Id	=	Utils::sanitizeText($_GET['emrICD10Code']);
			$sndIcd10Id	=	Utils::sanitizeText($_GET['sndEmrICD10Code']);			
			// 2.b Validate ICD10 
			$validatedIcd10Id = $this->tpProcessor->validateKeyInIcd10($icd10Id);
			$validatedSndIcd10Id = $this->tpProcessor->validateKeyInIcd10($sndIcd10Id);
			
			if($validatedIcd10Id['errorCode'] == 'OK'){
				$result['result']['firstIcd10'] = $validatedIcd10Id['mess'];
			}
			if($validatedSndIcd10Id['errorCode'] == 'OK'){
				$result['result']['secondIcd10'] = $validatedSndIcd10Id['mess'];
			}
			// error occur
			if($validatedIcd10Id['errorCode'] != 'OK'){
				$result['error']['code']	=	$validatedIcd10Id['errorCode'];
				$result['error']['mess']	=	$validatedIcd10Id['mess'];
				$result['error']['field']	=	'emrICD10Code' . $ticketId;
				return json_encode($result);
			}
			if($validatedSndIcd10Id['errorCode'] != 'OK'){
				$result['error']['code']	=	$validatedSndIcd10Id['errorCode'];
				$result['error']['mess']	=	$validatedSndIcd10Id['mess'];
				$result['error']['field']	=	'sndEmrICD10Code' . $ticketId;
				return json_encode($result);
			}//
			
			// 2.c If everything ok, save data
			$emr = array(
				'ticket_id'=>$ticketId,
	        	'doctor_id'=>$_GET['emrDoctorId'],
	        	'diagnosis'=>$_GET['emrDiagnosis'],
	        	'icd10_primary'=>$icd10Id,
				'icd10_secondary'=>$sndIcd10Id
			);
			$result['result']['inPatientEmrId']=$this->tpProcessor->saveEmerEmr($emr);
			 
		} catch (InpatientCoreException $ex) {			 
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionSaveGeneralDiagnosisAjax for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		} catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionSaveGeneralDiagnosisAjax for ticket [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
        }
		$this->logger->info('Finish actionSaveGeneralDiagnosisAjax for patient has ticket ['. $ticketId .']');
		return json_encode($result);
	}


	/**
	 * Add new vital sign for given patient
	 * @param $_GET['patientId']
	 * @param $_GET['examineTime']
	 * @return jsonObject patient info.
	 */
	public function actionAddVitalSign() {
		$result = array('error'=>array('code'=>0,'mess'=>''),'result'=>array());
		$patientId = $_GET['patientId'];
		 

		$this->logger->info('Start AddVitalSign for patient has patientId ['. $patientId .']');
		try {
			 
			$examineTime = $_GET['examineTime'];
			$result['result']['vitalSignId'] = $this->tpProcessor->addVitalSign($patientId, $examineTime);
			 
		} catch (InpatientCoreException $ex) {
			 
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('AddVitalSign for patient has patientId ['. $patientId .'] fail. Error detail: '. $ex->getMessage());
		}  catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('AddVitalSign for patient has patientId ['. $patientId .'] fail. Error detail: '. $ex->getMessage());
        }
		$this->logger->info('Finish AddVitalSign for patient has patientId ['. $patientId .']');
		return json_encode($result);
		 
	}

	/**
	 * ListVitalSign
	 * @param $_GET['patientId']
	 * @return jsonObject patient info.
	 */
	public function actionListVitalSign() {
		$result = array('error'=>array('code'=>0,'mess'=>''),'result'=>array());
		$patientId = $_GET['patientId'];
		$this->logger->info('Start actionListVitalSign for patient has patientId ['. $patientId .']');
		try {

			$result['result']['listVitalSign'] = $this->tpProcessor->listVitalSign($patientId);
			 
		} catch (InpatientCoreException $ex) {
			 
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('AddVitalSign for patient has patientId ['. $patientId .'] fail. Error detail: '. $ex->getMessage());
		} catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('AddVitalSign for patient has patientId ['. $patientId .'] fail. Error detail: '. $ex->getMessage());
        }
		$this->logger->info('Finish actionListVitalSign for patient has patientId ['. $patientId .']');
		return json_encode($result);
		 
	}

	/**
	 * save vital sign for given patient
	 * @param $_GET['ticketId']
	 * @return jsonObject patient info.
	 */
	public function actionSaveVitalSign() {
		$result = array('error'=>array('code'=>0,'mess'=>''),'result'=>array());
		$this->logger->info('Start SaveVitalSign ');
		try {
			$data = array(
        	'pulse'=>Utils::getNumberParaFromGet('vitalSignPulse'),
        	'blood_presure_s'=>Utils::getNumberParaFromGet('vitalSignBps'),
        	'blood_presure_d'=>Utils::getNumberParaFromGet('vitalSignBpd'),
        	'respiratory_rate'=>Utils::getNumberParaFromGet('vitalSignRespiratory'),
        	'temperature'=>Utils::getNumberParaFromGet('vitalSignTemperature'),
        	'sp02'=>Utils::getNumberParaFromGet('vitalSignSp02'),
        	'ticket_id'=>$_GET['ticketId']);
			 
			$this->tpProcessor->saveVitalSign($_GET['vitalSignId'], $data);

			 
		} catch (InpatientCoreException $ex) {
			 
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('SaveVitalSign fail. Error detail: '. $ex->getMessage());
		}  catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('SaveVitalSign fail. Error detail: '. $ex->getMessage());
        }
		$this->logger->info('Finish SaveVitalSign ');
		return json_encode($result);
		 
	}

	/**
	 * Delete vital sign.
	 * @param $_GET['vitalSignId']
	 * @param $_GET['patientId']
	 * @return jsonObject patient info.
	 */
	public function actionDelVitalSign() {
		$result = array('error'=>array('code'=>0,'mess'=>''),'result'=>array());
		$vitalSignId = $_GET['vitalSignId'];
		$patientId =  $_GET['patientId'];


		$this->logger->info('Start actionDelVitalSign ['. $vitalSignId .']');
		try {
			$this->tpProcessor->delVitalSign($vitalSignId);
			 
		} catch (InpatientCoreException $ex) {
			 
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = InPatientCoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionDelVitalSign  ['. $vitalSignId .'] fail. Error detail: '. $ex->getMessage());
		}  catch (EnumException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionDelVitalSign  ['. $vitalSignId .'] fail. Error detail: '. $ex->getMessage());
        }
		$this->logger->info('Finish actionDelVitalSign ['. $vitalSignId .']');
		return json_encode($result);
		 
		 
	}
}
?>